package com.chb.service.impl;

import com.chb.dao.BranchDao;
import com.chb.dao.InLibrayDao;
import com.chb.dao.OutLibrayDao;
import com.chb.model.entity.Branch;
import com.chb.model.entity.Staff;
import com.chb.model.vo.GoodsCountVo;
import com.chb.model.vo.Result;
import com.chb.model.vo.StaffPriceCountVo;
import com.chb.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author: 陈海彬
 * @date: Created in 2023/10/9 10:13
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private HttpSession session;
    @Autowired
    private InLibrayDao inLibrayDao;
    @Autowired
    private OutLibrayDao outLibrayDao;
    @Autowired
    private BranchDao branchDao;

    /**
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 获取统计信息所需的数据
     * @create time: 2023/10/9 10:15
     */
    @Override
    public Result getStatisticsInfo(Date begin, Date end) {
        System.out.println(begin);
        System.out.println(end);
        Staff staff = (Staff) session.getAttribute("staff");
        Map<String, Object> data = new HashMap<>();

        /**
         * 获取本店进销统计
         */
        //获取成本额
        BigDecimal costAmountCount = inLibrayDao.getCostAmountCount(staff.getBranchid(), begin, end);
        //获取营业额
        BigDecimal businessCount = outLibrayDao.getBusinessCount(staff.getBranchid(), begin, end);
        //本店进销统计数据
        Map<String, Object> salesStatisticsMap = new HashMap<>();
        salesStatisticsMap.put("costAmountCount", costAmountCount);
        salesStatisticsMap.put("businessCount", businessCount);


        /**
         * 获取各分店营销统计
         */
        List<StaffPriceCountVo> priceByBranch = outLibrayDao.getAllCountPriceByBranch(begin, end);
        List<Branch> branchList = branchDao.findAll(111);
        List branchNameAndValue = new ArrayList();
        for (Branch branch : branchList) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", branch.getBranchname());
            for (StaffPriceCountVo priceBranch : priceByBranch) {
                Integer priceBranchBranchid = priceBranch.getBranchid();
                if (priceBranchBranchid.equals(branch.getBranchid())) {
                    map.put("value", priceBranch.getPrice());
                    break;
                } else {
                    map.put("value", 0);
                }
            }
            branchNameAndValue.add(map);
        }


        /**
         * 获取各商品销量统计
         */
        List<GoodsCountVo> goodsCountByBranchId = outLibrayDao.getGoodsCountByBranchId(staff.getBranchid(), begin, end);
        Map goodsMap = new HashMap<>();
        List goodsNameList = new ArrayList<>();
        List goodsCountList = new ArrayList<>();
        for (GoodsCountVo goodsCountVo : goodsCountByBranchId) {
            goodsNameList.add(goodsCountVo.getGoodsname());
            goodsCountList.add(goodsCountVo.getCount());
        }
        goodsMap.put("goodsNameList", goodsNameList);
        goodsMap.put("goodsCountList", goodsCountList);


        data.put("salesStatisticsMap", salesStatisticsMap);
        data.put("allStatisticsList", branchNameAndValue);
        data.put("allGoodsMap", goodsMap);
        return Result.success(data);
    }
}